﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechnicalAnalysisEngine;
using System.Diagnostics;

namespace TechnicalAnalysisEngine.Tests
{
    [TestClass]
    public class TestSMA
    {
        List<double> inputValues;
        List<double> expectedMatureValues;

        int periods;
        int expectedResultCount;

        [TestInitialize]
        public void Initialize()
        {
            inputValues = new List<double>()
            {
                22.27,22.19,22.08,22.17,22.18,22.13,22.23,22.43,22.24,22.29,22.15,22.39,22.38,22.61,23.36,24.05,23.75,23.83,
                23.95,23.63,23.82,23.87,23.65,23.19,23.10,23.33,22.68,23.10,22.40,22.17
            };

            expectedMatureValues = new List<double>()
            {
                22.22,22.21,22.23,22.26,22.31,22.42,22.61,22.77,22.91,23.08,23.21,23.38,23.53,23.65,23.71,23.69,23.61,23.51,23.43,23.28,23.13
            };

            periods = 10;
            expectedResultCount = 21;
        }

        [TestMethod]
        public void TestSMAMatureValues()
        {
            var result = AnalysisEngine.SMA(inputValues, periods);

            Assert.AreEqual(expectedResultCount, result.Values.Count);

            bool testResult = true;
            string message = null;

            for (int i = 0; i < result.Values.Count; i++)
            {
                if (!TestHelpers.EqualsWithTolerance(result.Values[i], expectedMatureValues[i], 1))
                {
                    message = string.Format("SMA values do not match at position {0}, expected: {1}, actual: {2} ",
                        i, expectedMatureValues[i], result.Values[i]);

                    testResult = false;
                    break;
                }
            }

            Assert.IsTrue(testResult, message);
        }

        [TestMethod]
        public void TestSMAPerformance()
        {
            Random random = new Random();

            var randomlyGeneratedValues = new List<double>();

            for (int i = 0; i < 10000; i++)
            {
                randomlyGeneratedValues.Add(random.Next(85, 90));
            }

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            var result = AnalysisEngine.SMA(randomlyGeneratedValues, periods);

            stopWatch.Stop();
            Console.WriteLine(string.Format("SMA, {0} values, {1} μs", randomlyGeneratedValues.Count, stopWatch.ElapsedTicks / 10));
        }
    }
}
